<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="visible" width="1000px" v-if="visible" append-to-body
      :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :disabled="disabled">
        <el-form-item label="发放用户:" prop="">
          <el-upload class="upload-demo" :action="fileaction" :headers="headers" :data="fileData" :limit="1"
            :on-success="handleFileChange" :show-file-list="true" :file-list="fileList">
            <el-button type="primary" size="small">导入用户</el-button>
            <!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div> -->
          </el-upload>
        </el-form-item>
        <el-form-item :label="lableText" prop="issueAmount">
          <el-input v-model="form.issueAmount" placeholder="请输入数量" :style="{width: inputWidth}" />
        </el-form-item>
        <el-form-item label="用户手机号:" prop="">
          <el-input v-model="phone" placeholder="请输入用户手机号" :style="{width: inputWidth}" />
          <el-button type="primary" size="small" style="margin-left: 20px" @click="addUser" :loading="addLoading">添 加
          </el-button>
        </el-form-item>
        <el-form-item label="" prop="">
          <el-table :data="tabelList" v-loading="loading" element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.6)" element-loading-text="加载中" stripe border>
            <el-table-column label="序号" align="center" type="index" width="64px" />
            <el-table-column label="用户姓名" align="center" prop="customerName" :show-overflow-tooltip="true" />
            <el-table-column label="手机号" align="center" prop="customerPhone" :show-overflow-tooltip="true" />
            <el-table-column label="校验结果" align="center" prop="customerPhone" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span class="el-icon-success icon_yes" v-if="scope.row.customerName"></span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" @click="deleteView(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="sumitLoading">执行发放</el-button>
        <el-button @click="cancel" v-if="!disabled">取 消</el-button>
      </div>
      <!-- 导入失败弹窗 -->
      <el-dialog title="导入失败" :visible.sync="dialogVisible" width="500" append-to-body>
        <div>{{importMessage}}</div>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getToken
  } from "@/utils/auth";
  import {
    applyIssueInfo,
    addApplyIssueInfo,
    addApplyIssueExecute,
    deleteApplyIssueInfo
  } from "@/api/integralOperation";

  export default {
    // dicts: ['asset_card_type'],
    name: "Dialog",
    props: {
     
    },
    data() {
      var checkIssueAmount = (rule, value, callback) => { //销售价格校验
        let regNumber = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^\d\.\d{1,2}$)/; //大于0的数值且最多保留两位小数的数值
        if (value === '') {
          callback(new Error('请输入数量'));
        }else if (regNumber.test(value) == false || value == '0.00' || value == '0.0') {
          return callback(new Error('请填写大于0且最多保留两位小数的数值'));
        } else {
          return callback()
        }
      };
      return {
        labelWidth: '150px',
        inputWidth: '320px',
        dialogVisible: false, //导入失败弹窗
        loading: false,
        sumitLoading: false,
        importMessage: '', //导入失败原因
        lableText: '每人获椰分赠数量',
        id: '', //
        // 是否禁用表单
        disabled: false,
        // 弹出层标题
        title: "发放积分",
        // 是否显示弹出层
        visible: false,
        // 导入所需参数
        addLoading: false,
        fileData: {
          applyId: '',
        },
        fileList: [],
        fileaction: process.env.VUE_APP_BASE_API + "/platform/asset/apply/issue/info/import",
        headers: {
          "X-Access-Token": getToken(),
        }, //设置上传文件请求头
        tabelList: [],
        isImplement: false, //是否执行
        phone: '',
        // 表单参数
        form: {
          issueAmount: '', //每人发放数量
          applyId: '', //
        },
        // 表单校验
        rules: {
          issueAmount: [
          {
            required: true,
            validator: checkIssueAmount,
            trigger: ['blur', 'change']
          }, 
        ],
        },
      };
    },
    created() {
      
    },
    methods: {
      //查询用户列表
      getCustomerLs(id) {
        
        this.loading = true
        applyIssueInfo(id).then(response => {
          this.tabelList = response.result
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      /** 打开弹框 */
      open(id,insType) {
        this.getCustomerLs(id)
        this.reset();
        this.id = id
        this.fileData.applyId = id
        this.visible = true
        if(insType == 1){
          this.lableText = '每人获CoCo赠数量'
        }else if(insType == 2){
          this.lableText = '每人获椰分赠数量'
        }
      },
      // 取消按钮
      cancel() {
        this.visible = false;
        this.reset();
      },
      // 表单重置
      reset() {
        // 子表选中数据
        this.sumitLoading = false
        this.form = {
          issueAmount: '', //每人发放数量
          applyId: '', //
        }
      },
      //导入文件
      handleFileChange(res, file) {
        console.log('文件', res, file)
        if (res.code == 200) {
          this.$message.success('导入成功');
          this.getCustomerLs(this.id)
        } else {
          // this.$message.error(res.message);
          // this.$message.error('导入失败')
          this.importMessage = res.message
          this.dialogVisible = true
          setTimeout(() => {
            this.fileList = []
          }, 800)
        }
      },
      /** 删除提交按钮deleteWhite */
      deleteView(row) {
        this.$confirm('是否删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteApplyIssueInfo(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getCustomerLs(this.id)
          });

        }).catch(() => {});
      },
      /** 执行按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.sumitLoading = true
            this.form.applyId = this.id
            addApplyIssueExecute(this.form).then(response => {
              this.$message.success('执行成功');
              this.cancel();
              this.$emit('refresh')
            }).catch(() => {
              this.sumitLoading = false;
            });
          }
        });
      },
      /** 添加手机号按钮 */
      addUser() {
        if (this.phone == '') {
          this.$message.error('请输入手机号');
          return
        }
        this.addLoading = true
        let data = {
          phone: this.phone,
          applyId: this.id
        }
        addApplyIssueInfo(data).then(response => {
          this.$message.success('添加成功');
          this.getCustomerLs(this.id)
          this.phone = ''
          this.addLoading = false
        }).catch(() => {
          this.addLoading = false
        });
      },
    }
  };

</script>
<style scoped>
  .main {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
  }

  .icon_yes {
    font-size: 28px;
    color: #67c23a;
  }

</style>
